package audit

import (
	"counter-help/admin/server/global"
	"counter-help/admin/server/model/audit"
	"time"
)

type AfterSalesService struct{}

// 创建售后
func (s *AfterSalesService) CreateAfterSales(afterSales audit.AfterSales) error {
	afterSales.Status = "申请中"
	return global.GVA_DB.Create(&afterSales).Error
}

// 撤销售后
func (s *AfterSalesService) CancelAfterSales(afterSalesID string) error {
	return global.GVA_DB.Model(&audit.AfterSales{}).Where("id = ?", afterSalesID).
		Update("status", "已撤销").Error
}

// 审核通过
func (s *AfterSalesService) ApproveAfterSales(afterSalesID string) error {
	approvedAt := time.Now()
	return global.GVA_DB.Model(&audit.AfterSales{}).Where("id = ?", afterSalesID).
		Updates(map[string]interface{}{"status": "通过审核", "approved_at": &approvedAt}).Error
}

// 审核不通过
func (s *AfterSalesService) RejectAfterSales(afterSalesID, reason, detailedReason string) error {
	rejectedAt := time.Now()
	return global.GVA_DB.Model(&audit.AfterSales{}).Where("id = ?", afterSalesID).
		Updates(map[string]interface{}{
			"status":          "审核不通过",
			"rejected_reason": reason,
			"detailed_reason": detailedReason,
			"rejected_at":     &rejectedAt,
		}).Error
}

// 对方同意
func (s *AfterSalesService) AgreeAfterSales(afterSalesID string) error {
	agreementReachedAt := time.Now()
	return global.GVA_DB.Model(&audit.AfterSales{}).Where("id = ?", afterSalesID).
		Updates(map[string]interface{}{"status": "双方同意", "agreement_reached_at": &agreementReachedAt}).Error
}

// 退款操作
func (s *AfterSalesService) RefundAfterSales(afterSalesID string, refundAmount float64) error {
	refundedAt := time.Now()
	return global.GVA_DB.Model(&audit.AfterSales{}).Where("id = ?", afterSalesID).
		Updates(map[string]interface{}{
			"is_refunded":   true,
			"refund_amount": refundAmount,
			"refunded_at":   &refundedAt,
			"status":        "售后成功",
		}).Error
}
